package org.jhulk.tests;

import org.jhulk.utils.TileMapUtils;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class TileMapTest extends BasicGame
{
	private TiledMap map;
	
	private String mapName;
	
	private int clkMapX = 0;
	
	private int clkMapY = 0;
	
	private int clkTileX = 0;
	
	private int clkTileY = 0;
	
	private int hovMapX = 0;

    private int hovMapY = 0;
	
    private int hovTileX = 0;

    private int hovTileY = 0;
	
	private int clickedTileId = 0;
	
	private int hoverTileId = 0;
	
	private double mapXForTile = 0;
	
	private double mapYForTile = 0;
	
	public TileMapTest() {
		super("Tile Map Test");
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
		map = new TiledMap("data/maps/testTileset.tmx");
				
		// read some properties from map and layer
		mapName = map.getMapProperty("title", "Unknown map name");
	}

	@Override
	public void update(GameContainer container, int delta) 
		throws SlickException {}

	@Override
	public void render(GameContainer container, Graphics g) 
		throws SlickException 
	{
		map.render(0, 0);
		
		//g.scale(0.35f,0.35f);
		//map.render(1400, 0);
		//g.resetTransform();
		
		g.drawString("Map name: " + mapName, 130, 12);
		
		//g.drawString("Map height: " + map.getHeight(), 510, 50);
		g.drawString("Tile height: " + map.getTileHeight(), 510, 50);
		g.drawString("Tile width: " + map.getTileWidth(), 510, 70);
		
		g.drawLine(510, 95, 775, 95);
		
		g.drawString("Click map coords: " + clkMapX + ", " + clkMapY, 510, 100);
		g.drawString("Click tile coords: " + clkTileX + ", " + clkTileY, 510, 120);
		
		g.drawString("Hover map coords: " + hovMapX + ", " + hovMapY , 510, 140);
		g.drawString("Hover tile coords: " + hovTileX + ", " + hovTileY, 510, 160);
		
		g.drawString("Clicked tile id " + clickedTileId, 510, 180);
		g.drawString("Hover tile id " + hoverTileId, 510, 200);
		
		g.drawString("MapX edge of current tile: " + mapXForTile, 510, 220);
		g.drawString("MapY edge of current tile: " + mapYForTile, 510, 240);
		
	}
	
	public void keyPressed(int key, char c) {
		if (key == Input.KEY_ESCAPE) {
			System.exit(0);
		}
	}
	
	public void mouseReleased(int button, int x, int y) 
	{
		clkMapX = x;
		clkMapY = y;
		
		clkTileX = TileMapUtils.getTileXfromMapX(x, map);
		clkTileY = TileMapUtils.getTileYfromMapY(y, map);
		
		clickedTileId = map.getTileId(clkTileX, clkTileY, 0);
	}

	
	 public void mouseMoved(int oldx, int oldy, int newx, int newy)
	 {
		 hovMapX = newx;
	     hovMapY = newy;
	     
	     hovTileX = TileMapUtils.getTileXfromMapX(newx, map);
	     hovTileY = TileMapUtils.getTileYfromMapY(newy, map);
	     
	     hoverTileId = map.getTileId(hovTileX, hovTileY, 0);
	     
	     mapXForTile = TileMapUtils.getTileXBorderFromMapX(newx, map);
	     mapYForTile = TileMapUtils.getTileYBorderFromMapY(newy, map);
	 }

	
	public static void main(String[] argv) {
		try {
			AppGameContainer container = new AppGameContainer(new TileMapTest());
			container.setDisplayMode(800,600,false);
			container.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}


}
